﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.HVAC.PipeSetting
{
	public partial class FrmAddPiping : HYBaseForm
	{
		public SystemTypeInfo OutputInfo
		{
			get
			{
				return this.outputInfo;
			}
			set
			{
				this.outputInfo = value;
			}
		}

		public FrmAddPiping()
		{
			this.InitializeComponent();
		}

		public FrmAddPiping(SystemTypeInfo systemType, IList<string> existSystemNames) : this()
		{
			if (systemType == null || existSystemNames == null)
			{
				throw new Exception("新建系统传入参数有误！");
			}
			this.inputInfo = systemType;
			this.existSystemNames = existSystemNames;
		}

		private void FrmAddPiping_Load(object sender, EventArgs e)
		{
			IList<string> linePatternName = PipeSettingManager.GetLinePatternName();
			IList<string> lineWeight = PipeSettingManager.GetLineWeight();
			IList<string> pipeTypeNames = PipeSettingManager.GetPipeTypeNames();
			IList<string> pipingClassify = PipeSettingManager.PipingClassify;
			this.InitComboBoxColumn(this.cbbLineWidth, lineWeight);
			this.cbbLineWidth.Items.Add("默认");
			this.InitComboBoxColumn(this.cbbLinePattern, linePatternName);
			this.cbbLinePattern.Items.Add("默认");
			this.InitComboBoxColumn(this.cbbSysType, pipeTypeNames);
			this.InitComboBoxColumn(this.cbbSysClassify, pipingClassify);
			base.Icon = Resource.YArchitechIcon;
			this.Text = "新建系统";
			this.txtSysName.Text = this.inputInfo.SysType;
			this.txtSysName.SelectAll();
			this.txtAbb.Text = this.inputInfo.Abbreviation;
			if (!string.IsNullOrEmpty(this.inputInfo.PipeType))
			{
				this.cbbSysType.SelectedItem = this.inputInfo.PipeType;
			}
			this.txtLineColor.Text = this.inputInfo.LineColor;
			System.Drawing.Color xmldataColor = SystemManageHelper.getXMLDataColor(this.inputInfo.LineColor);
			this.txtLineColor.ForeColor = xmldataColor;
			if (!string.IsNullOrEmpty(this.inputInfo.LinePattern))
			{
				this.cbbLinePattern.SelectedItem = this.inputInfo.LinePattern;
			}
			if (!string.IsNullOrEmpty(this.inputInfo.LineWeight))
			{
				this.cbbLineWidth.SelectedItem = this.inputInfo.LineWeight;
			}
			if (!string.IsNullOrEmpty(this.inputInfo.SysCategory))
			{
				this.cbbSysClassify.SelectedItem = this.inputInfo.SysCategory;
			}
			this.txtSysGroup.Text = this.inputInfo.GroupName;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			string text = this.txtSysName.Text.Trim();
			string text2 = this.txtAbb.Text.Trim();
			string text3 = this.txtSysGroup.Text.Trim();
			if (string.IsNullOrEmpty(text))
			{
				YJKMessageBox.Show("系统类型不能为空，请重新输入！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.txtSysName.SelectAll();
				return;
			}
			if (this.existSystemNames.Contains(text))
			{
				YJKMessageBox.Show("已存在相同系统类型！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.txtSysName.SelectAll();
				return;
			}
			if (string.IsNullOrEmpty(text2))
			{
				YJKMessageBox.Show("系统缩写不能为空，请重新输入！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.txtAbb.SelectAll();
				return;
			}
			if (string.IsNullOrEmpty(text3))
			{
				YJKMessageBox.Show("系统组名不能为空，请重新输入！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.txtSysGroup.SelectAll();
				return;
			}
			if (this.outputInfo == null)
			{
				this.outputInfo = new SystemTypeInfo();
			}
			this.outputInfo.SysType = text;
			this.outputInfo.Abbreviation = text2;
			this.outputInfo.GroupName = text3;
			this.outputInfo.PipeType = ((this.cbbSysType.SelectedItem == null) ? string.Empty : this.cbbSysType.SelectedItem.ToString());
			this.outputInfo.LineColor = this.txtLineColor.Text;
			this.outputInfo.LinePattern = ((this.cbbLinePattern.SelectedItem == null) ? string.Empty : this.cbbLinePattern.SelectedItem.ToString());
			this.outputInfo.LineWeight = ((this.cbbLineWidth.SelectedItem == null) ? string.Empty : this.cbbLineWidth.SelectedItem.ToString());
			this.outputInfo.SysCategory = ((this.cbbSysClassify.SelectedItem == null) ? string.Empty : this.cbbSysClassify.SelectedItem.ToString());
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void cbbSysType_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void cbbLinePattern_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void cbbLineWidth_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void cbbSysClassify_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void txtSysName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				this.btnOK_Click(sender, e);
			}
		}

		private void txtLineColor_Click(object sender, EventArgs e)
		{
			string colorStr = this.txtLineColor.Text.Trim();
			this.ShowColorDialog(colorStr);
		}

		private void ShowColorDialog(string colorStr)
		{
			ColorDialog colorDialog = new ColorDialog();
			colorDialog.AllowFullOpen = true;
			colorDialog.FullOpen = true;
			System.Drawing.Color xmldataColor = SystemManageHelper.getXMLDataColor(colorStr);
			if (!xmldataColor.IsKnownColor)
			{
				int[] array = new int[16];
				array[0] = SystemManageHelper.GetIntAboutColor(xmldataColor);
				colorDialog.CustomColors = array;
			}
			colorDialog.Color = xmldataColor;
			DialogResult dialogResult = colorDialog.ShowDialog();
			if (DialogResult.OK == dialogResult)
			{
				string newRGB = SystemManageHelper.getNewRGB(colorDialog.Color.R);
				string newRGB2 = SystemManageHelper.getNewRGB(colorDialog.Color.G);
				string newRGB3 = SystemManageHelper.getNewRGB(colorDialog.Color.B);
				string str = string.Concat(new string[]
				{
					"RGB ",
					newRGB.ToString(),
					" ",
					newRGB2.ToString(),
					" ",
					newRGB3.ToString()
				});
				string text = "█" + str;
				this.txtLineColor.Text = text;
				System.Drawing.Color xmldataColor2 = SystemManageHelper.getXMLDataColor(text);
				this.txtLineColor.ForeColor = xmldataColor2;
			}
		}

		private void InitComboBoxColumn(System.Windows.Forms.ComboBox column, IList<string> dataList)
		{
			int num = 0;
			string text = "";
			column.Items.Clear();
			if (dataList == null)
			{
				return;
			}
			foreach (string text2 in dataList)
			{
				if (text2.Length > num)
				{
					num = text2.Length;
					text = text2;
				}
				column.Items.Add(text2);
			}
			int dropDownWidth = DataGridViewCell.MeasureTextWidth(base.CreateGraphics(), text, this.Font, 50, TextFormatFlags.Default);
			column.DropDownWidth = dropDownWidth;
		}

		private void txtLineColor_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\r')
			{
				e.Handled = true;
			}
		}

		private void txtLineColor_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyData != Keys.Return)
			{
				e.Handled = true;
			}
		}

		private SystemTypeInfo inputInfo;

		private IList<string> existSystemNames;

		private SystemTypeInfo outputInfo;
	}
}
